use "Data\MFPS Child Health Paper Replication Data - Child Level.dta", clear

* Set Panel Variables
xtset caseid_n year

/*create dummy variables for missing data
local cov cluster_num work_bl ever_use_bl curr_use_bl sex_age_bl age_group_bl edu_primary_bl birth_order religion_r_bl ethnicity_r_bl total_birth_bl child_sex wealthscore_bl child_age_months chi1m 
foreach i of local cov{ 
	gen `i'_missing = 0 
	replace `i'_missing = 1 if `i'==.
	replace `i' = 0 if `i'==.
}
*/ 
* Create global of covariates
global covariates "i.cluster_num work_bl ever_use_bl curr_use_bl sex_age_bl i.age_group_bl edu_primary_bl i.birth_order religion_r_bl ethnicity_r_bl i.total_birth_bl child_sex"

*Create global of age covariates
mkspline age1 6 age2 12 age3 18 age4 24 age5 30 age6 = child_age_months
global agevars "i.chi1m age1-age6"

forvalues i = 1/6{
	gen chi_age_group_`i' = age`i'>0
}

* Generate Variable for first born child
gen first_born = (birth_order==1)


local outcome haz06 credi_z_score
local het child_sex first_born wantanother_bl

local i = 1
foreach x of local het{ 
	if `i' == 1{ 
		local name="gender"
	}
	if `i' == 2{ 
		local name="First Born"
	}
	if `i'==3 {
		local name="Want Another Child"
	}
		
		
	local j=1
	foreach y of local outcome { 
		if `j'==1{
			global agevars "i.chi1m age1-age6" 
			local year = 2017
			local stub = "haz"
			local name2 = "HAZ"
		}
		if `j'==2{
			global agevars "age1-age6" 
			local year = 2018
			local stub = "cred"
			local name2 = "CREDI"
		}
		
		
		* Index results with heterogeneous effects
		eststo `stub'1: qui reg  `y' treatment  $agevars if year==`year' & index_dummy==1 ///
			& born_during_study==0 & `x'==0, robust
			
		eststo `stub'2: qui reg  `y' treatment  $agevars $covariates if year==`year' & index_dummy==1 ///
			& born_during_study==0 & `x'==0, robust
			
		eststo `stub'3: qui reg  `y' treatment  $agevars if year==`year' & index_dummy==1 ///
			& born_during_study==0 & `x'==1, robust
			
		eststo `stub'4: qui reg  `y' treatment  $agevars $covariates if year==`year' & index_dummy==1 ///
			& born_during_study==0 & `x'==1, robust
			
		local j = `++j'
	}
	
	* Table Results
	esttab haz1 haz2 haz3 haz4 cred1 cred2 cred3 cred4 ///
	using "Results\Heterogeneous Effects\Heterogeneous Effects `name' (ITT).rtf", ///
	replace se ///
	scalar(N r2 F)  mlabels("HAZ (null, unadjust)" "HAZ (null, adjust)" "HAZ(condition, unadjust)" "HAZ (condition, adjust)" "CREDI Z (null, unadjust)" "CREDI  Z (null, adjust)" "CREDI Z (condition, unadjust)" "CREDI Z (condition, adjust)") collabels(none) ///
	starlevels(* .1 ** .05 *** .01)     ///
	compress keep(treatment _cons age1 age2 age3 age4 age5 age6 1.age_group_bl 2.age_group_bl 3.age_group_bl 1.total_birth_bl 2.total_birth_bl 3.total_birth_bl 4.total_birth_bl 5.total_birth_bl 6.total_birth_bl 7.total_birth_bl work_bl edu_primary_bl religion_r_bl ethnicity_r_bl 1.birth_order 2.birth_order 3.birth_order 4.birth_order 5.birth_order 6.birth_order 7.birth_order) ///
	order(treatment )
	
	local i = `++i'
}